function comprova(refFormulari){
    var estat=true;
    document.getElementById("loading").innerHTML = "";
    //alert(document.getElementById('E-mail').value + " està buit");
    //mirem si alqun camp està buit
    if(document.getElementById('E-mail').value == ""){
        //alert(document.getElementById('E-mail').value + " està buit");						
        actual = document.getElementById('E-mail');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" *" + document.getElementById('E-mail').id + " es obligatori");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('E-mail').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('E-mail').id).appendChild(miSpan);
        document.getElementById('E-mail').style.background = "red";
        estat = false;
    }else{
        document.getElementById('label'+document.getElementById('E-mail').id).innerHTML = document.getElementById('E-mail').id + ":";
        document.getElementById('E-mail').style.background = "#cfea7f";
    }
                
    if(document.getElementById('Password').value == ""){
        actual = document.getElementById('Password');
        miSpan=document.createElement("span");
        miSpan.setAttribute("style","color:red");
        miText = document.createTextNode(" *" + document.getElementById('Password').id + " es obligatori");
        miSpan.appendChild(miText);
        document.getElementById('label'+document.getElementById('Password').id).innerHTML = "";
        document.getElementById('label'+document.getElementById('Password').id).appendChild(miSpan);
        document.getElementById('Password').style.background = "red";
        //alert(estat);          
        estat = false;
    } else{
        document.getElementById('label'+document.getElementById('Password').id).innerHTML = document.getElementById('Password').id + ":";
        document.getElementById('Password').style.background = "#cfea7f";
    }

    //el mail requereix un tractament especial
    if(estat){
        var expresio = new RegExp(/^[a-zA-Z0-9!#$%&?^\/_'{}|~=*+-]+(\.[a-zA-Z0-9!#$%&?^\/_'{}|~=*+-]+)*@([a-zA-Z0-9\-])+(\.([a-zA-Z0-9\-])+)+$/);
	mail = document.getElementById('E-mail').value;
	if(expresio.test(mail)){
            document.getElementById('labelE-mail').innerHTML = "E-mail";
            document.getElementById('E-mail').style.background = "#cfea7f";
        } else{
            miSpan=document.createElement("span");
            miSpan.setAttribute("style","color:red");
            miText = document.createTextNode(" * E-mail introduït es incorrecte");
            miSpan.appendChild(miText);
            document.getElementById('labelE-mail').innerHTML = "";
            document.getElementById('labelE-mail').appendChild(miSpan);
            document.getElementById('E-mail').style.background = "red";
            estat = false;	
	}	
    }
    
    if(estat) {
        checkUserAjax(refFormulari);
    }
    
    return false;
}

function checkUserAjax(refForm) {
    var infoContacto = new Array();
    
    for(var i = 0; i<refForm.getElementsByTagName("input").length; i++) {
        if(refForm.getElementsByTagName("input")[i].name == "E-mail") infoContacto[0] = refForm.getElementsByTagName("input")[i].value;
        else if(refForm.getElementsByTagName("input")[i].name == "Password") infoContacto[1] = refForm.getElementsByTagName("input")[i].value;
    }
    
    //Comienzo peticion Ajax
    var peticion_http = null;
    
    if(window.XMLHttpRequest) peticion_http = new XMLHttpRequest();
    else if(window.ActiveXObject) peticion_http = new ActiveXObject("Microsoft.XMLHTTP");
    document.getElementById("loading").innerHTML = "<img   src='../../projecte-sintesi-dani-victor/site_media/img/ajax-loader.gif'>";
   
    peticion_http.onreadystatechange = getDisponibilitatContacto;

    peticion_http.open("POST", "../projecte-sintesi-dani-victor/site_media/php/usuari_check_exists.php", true);
    peticion_http.setRequestHeader("Content-type","application/x-www-form-urlencoded");
    peticion_http.send("E-mail="+infoContacto[0]+"&Password="+infoContacto[1]);
    
    function getDisponibilitatContacto() {
        
        if(peticion_http.readyState == 4) {
            if(peticion_http.status == 200) {
                if(peticion_http.responseText == "Usuario EXISTENTE") {
                    //alert("Usuario EXISTENTE");
                    document.getElementById("loading").innerHTML = "";
                    refForm.submit();
                }
                else if(peticion_http.responseText == "Usuario NO EXISTENTE") {                   
                    document.getElementById("loading").innerHTML = "";
                        miSpan=document.createElement("span");
                        miSpan.setAttribute("style","color:red");
                        miText = document.createTextNode(" * E-mail/password introduït es incorrecte");
                        miSpan.appendChild(miText);
                        document.getElementById('loading').innerHTML = "";
                        document.getElementById('loading').appendChild(miSpan);
                        
                }
            }
        }
    }    
}




